package com.by.util;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;


public class DataConvertor {
	public static ObjectMapper mapper = new ObjectMapper();
	public static SimpleDateFormat dateTime = new SimpleDateFormat(
			"yyyy-MM-dd hh:mm:ss");
	public static SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
	public static SimpleDateFormat time = new SimpleDateFormat("hh:mm:ss");

	/**
	 * 
	 * 将对象转换为JSON格式的数据
	 * 
	 * @param obj
	 * @return
	 */
	public static String object2Json(Object obj) {
		try {
			return mapper.writeValueAsString(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public static Map<String, Object> json2Map(String json) {
		try {
			return mapper.readValue(json, Map.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static Integer parseInteger(String s) {
		try {
			return s == null ? null : Integer.parseInt(s);
		} catch (Exception e) {
			// 不处理，返回null
		}
		return null;
	}

	public static String parseString(String s) {
		// 为了方便程序的编写
		return s;
	}

	public static Byte parseByte(String s) {
		try {
			return s == null ? null : Byte.parseByte(s);
		} catch (Exception e) {
			// 不处理，返回null
		}
		return null;
	}

	public static Long parseLong(String s) {
		try {
			return s == null ? null : Long.parseLong(s);
		} catch (Exception e) {
			// 不处理，返回null
		}
		return null;
	}

	// BigDecimal
	public static BigDecimal parseBigDecimal(String s) {
		try {
			return s == null ? null : new BigDecimal(s);
		} catch (Exception e) {
			// 不处理，返回null
		}
		return null;
	}

	public static Date parseDateTime(String s) {
		try {
			return dateTime.parse(s);
		} catch (Exception e) {
			// 不处理，返回null
			e.printStackTrace();
		}
		return null;
	}

	public static Date parseDate(String s) {
		try {
			return date.parse(s);
		} catch (Exception e) {
			// 不处理，返回null
			e.printStackTrace();
		}
		return null;
	}

	public static Date parseTime(String s) {
		try {
			return time.parse(s);
		} catch (Exception e) {
			// 不处理，返回null
			e.printStackTrace();
		}
		return null;
	}

	public static Date parseTimestamp(String s) {
		if (s != null && s.length() > 0) {
			try {
				return s == null ? null : new Date(Long.parseLong(s));
			} catch (Exception e) {
				// 不处理，返回null
				e.printStackTrace();
			}
		}
		return null;
	}

	public static Map<String, String> List2Map(List<Object> list, String key,
			String value) {
		Map<String, String> map1 = new LinkedHashMap<String, String>();
		for (Object mm : list) {
			@SuppressWarnings("unchecked")
			Map<String, String> m = (Map<String, String>) mm;
			String k = "";
			String v = "";
			for (String item : m.keySet()) {
				if (key.equals(item)) {
					k = (String) m.get(item);
				}
				if (value.equals(item)) {
					v = (String) m.get(item);
				}
			}
			map1.put(k, v);

		}
		return map1;
	}
}
